package com.longai.controller;

import java.awt.Image;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import util.Page;
import util.ToJson;

import com.alibaba.fastjson.JSON;
import com.longai.model.Momentlike;
import com.longai.model.User;
import com.longai.service.FileUpload;
import com.longai.service.MomentlikeService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.longai.model.UploadFileInfo;
import com.longai.model.User;
//import com.longai.service.FileUpload;

import com.longai.service.UserService;

import functions.GetProperties;
import functions.Px;

@Controller
@RequestMapping("user")
public class UserAction {
	@Autowired
	UserService userService;
	FileUpload fileupload;

	// 登录验证
	//参数name用户名，pwd密码
	//返回user对象，失败为空
	@RequestMapping(value = "login", method = RequestMethod.GET)
	@ResponseBody
	public Object loginPost(@RequestParam(value = "name") String name,
			@RequestParam(value = "pwd") String password) {
		try {
			List<User> u= userService.valid(name);
			User user = u.get(0);
			if(user.getPassword().equals(password))
			return user;
			else 
				return "";
			
		} catch (Exception e) {
			return "";
		}
		
	
		
	}

	@RequestMapping(value = "register", method = RequestMethod.GET)//注册
	@ResponseBody
	public Object register(@RequestParam("user")String str) {
		String error = "error";
		
		try {
			Gson gson = new Gson();
			User user = gson.fromJson(str, User.class);
			//user = JSON.parseObject(str, User.class);
			System.out.println(user.getName());
			try {
				User userFromDB = userService.valid(user.getName()).get(0);
				return "";
			} catch (Exception e) {
				userService.save(user);
				return user;
			}	
			
				
		} catch (Exception e) {
			System.out.println(e);
			return "";
		}

	}

	// ********************************************************************************//
	// author by miao



	// author by miao
	// 上传头像，地址写入数据库
	@RequestMapping(value = "userIcon", method = RequestMethod.POST)
	@ResponseBody
	public String handleFileUpload(@RequestParam("userIcon") MultipartFile file,@RequestParam("userId")String userId,
			HttpServletRequest request) throws IOException {
		//String userId = "3";
		User user = userService.findById(Integer.parseInt(userId));
		UploadFileInfo uploadFileInfo = fileupload.uploadUserIcon(file, request, user);
		
		if (uploadFileInfo.isStatus()) {
			try {
				System.out.println(userId);
				user.setProfilepic(uploadFileInfo.getName());
				userService.save(user);
				String urlString = uploadFileInfo.getName();
				System.out.println(uploadFileInfo.isStatus());
				return urlString;
			} catch (Exception e) {
				System.out.println(e);
				return "";
			}
			
		}
		else {
			return "";
		}
		
	}
	
	// insertDetail
	// pram author by miao
	@RequestMapping(value="insertDetail",method = RequestMethod.GET)
	@ResponseBody
	public String insertDetail(@RequestParam("userDetail") String userDetail) {
		// 用户资料上传属性name,nickname,email,biography,gender;
		// userDetail转成对象后首先查找Id
		Gson gson = new Gson();
		User userChange = gson.fromJson(userDetail, User.class);
		try {
			int id = userChange.getUserId();
			User newUser = userService.findById(id);
			newUser.setNickname(userChange.getNickname());
			newUser.setEmail(userChange.getEmail());
			newUser.setBiography(userChange.getBiography());
			newUser.setGender(userChange.getGender());
			newUser.setBiography(userChange.getBiography());
			newUser.setPhone(userChange.getPhone());
			newUser.setType(userChange.getType());
			
			userService.save(newUser);
			
			return Integer.toString(id);
		} catch (Exception e) {
			System.out.println(e);
			return "0";
		}
		
	}

	// getDetail
	// pram author by miao
	// 获取用户资料接口
		@RequestMapping("getUser")
		public String getUser(Model model){
			List<User> users=userService.findAll();
			model.addAttribute("users",users);
			return "userManager";
		}

	@RequestMapping(value = "getDetail",method = RequestMethod.GET)
	@ResponseBody
	public Object getDetail(@RequestParam("userId") String userId) {
		
		//try {
			User user2 = userService.findById(Integer.parseInt(userId));
			user2.setProfilepic(user2.getProfilepic());
//			Gson gson=new Gson();
//			String a="233";
//			String bString= ToJson.toJson(a);
//			System.out.println(ToJson.toJson(bString));
//			System.out.println(gson.fromJson(bString, String.class));
			return user2;
		//} catch (Exception e) {
			
		// "";
			
	//	}


	}
	//author: pengshzihong
	@RequestMapping("findLover")
	@ResponseBody
	public Object findLover(@RequestParam("gens") double gens,@RequestParam("currentPage") String page,@RequestParam("amount") String amount) throws IOException{
		Page users=userService.getLover(gens,page,amount);
		if(users.getList().isEmpty()){
			return "";
		}
		else{
			return users;
		}
	}

}
